package com.face.receive.jsu;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.json.JSONObject;

import com.face.bean.jsu.LostPopulationMessage;
import com.face.operate.jsu.LostMessage;
import com.face.operate.jsu.LostToken_Operate;
@WebServlet("/GetLostList")
public class GetLostList extends HttpServlet {

	String url = "http://47.106.35.203:8080/lost/";
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String username = request.getParameter("username");
		
		LostMessage lostMessage = new LostMessage();
		LostToken_Operate lost = new LostToken_Operate();
		JSONArray jArray = new JSONArray();
		
		List<LostPopulationMessage> messageList = lostMessage.selectByUsername(username);
	    for(LostPopulationMessage message:messageList){
	    	net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(message);
	    	String name = message.getName();
	    	List<String> urlList = lost.selectPathByUserNameAndName(username, name);
	    	JSONArray jsonArray = new JSONArray();
	    	for(String s:urlList){
	    		jsonArray.add(url+s);
	    	}
	    	jsonObject.put("url", jsonArray);
	    	jArray.add(jsonObject);
	    }
	    
	    response.setContentType("application/json; charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(jArray.toString());
		System.out.println("返回的json数据：" + jArray.toString());
	}

}
